const localData = localStorage.getItem('pz_v3pz')

const state = localData ? localData.menu : {
  isCollapse: false,
  selectMenu: [],
  routerList: [],
  menuActive: '1-1'
}

const mutations = {
  collapseMenu(state) {
    state.isCollapse = !state.isCollapse
  },
  addMeun(state, paylad) {
    if (state.selectMenu.findIndex(item => item.path === paylad.path) === -1) {
      state.selectMenu.push(paylad)
    }
  },
  closeMenu(state, paylad) {
    const index = state.selectMenu.findIndex(item => item.name === paylad.name)
    state.selectMenu.splice(index, 1)
  },
  dynamicMenu(state, paylad) {
    // 通过Glob 导入文件
    const modules = import.meta.glob('../views/**/**/*.vue')

    function routerSet(router) {
      router.forEach(item => {
        // 判断是否存在子菜单
        if (!item.children) {
          const url = `../views${item.meta.path}/index.vue`
          // 获取到vue组件
          item.component = modules[url]
        } else {
          routerSet(item.children)
        }
      })
    }
    routerSet(paylad)
    // 拿到完整的路由数据
    state.routerList = paylad
  },
  updateMenuActive(state, paylad) {
    state.menuActive = paylad
  }
}

export default {
  state,
  mutations
}
